﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using DataAccessComponents;
using BusinessLogicLayer;
using System.IO;

namespace inzDesktop
{
    /// <summary>
    /// Interaction logic for windowEmailer.xaml
    /// </summary>
    public partial class windowEmailer : Window
    {
        private ObservableCollection<Klient> listaKlientow;
        private WPFEmailer wpfEmailer = new WPFEmailer();
        private DaneFirma daneFirma;
        public windowEmailer()
        {
            InitializeComponent();
        }

        public void inicjalizujZawartosc(ObservableCollection<Klient> listaKlientow){
            
            //dane firmy
            DaneFirmaBL daneBL = new DaneFirmaBL();
            daneFirma = daneBL.pobierz("");
            tbOd.Text = daneFirma.Email_smtp;

            this.listaKlientow = listaKlientow;
            dataGridDo.ItemsSource = this.listaKlientow;            
            lblDo.Content = "Do:\n(" + listaKlientow.Count + ")";
            this.DataContext = wpfEmailer;
        }
        
        private void btnWyslij_Click(object sender, RoutedEventArgs e)
        {            
            //wpfEmailer = new WPFEmailer();
            wpfEmailer.From = daneFirma.Email_smtp;                                  

            // dane serwera , najlepiej pobierać gdzieś z systemu
            wpfEmailer.Host = daneFirma.Host_smtp.Remove(daneFirma.Host_smtp.IndexOf("http://"),7);
            
            wpfEmailer.UseSSL = false;
            wpfEmailer.User = daneFirma.Login_smtp;
            wpfEmailer.Password = daneFirma.Haslo_smtp;

            // Funkcja wywoływana po poprawnym wysłaniu email
            wpfEmailer.client.SendCompleted += new System.Net.Mail.SendCompletedEventHandler(client_SendCompleted);

            Dictionary<String, String> odbiorcy = new Dictionary<string, string>();
            foreach (Klient klient in listaKlientow)
            {
                odbiorcy.Add(String.Format("{0} {1}", klient.Imie, klient.Nazwisko), klient.Email);                
            }           
            
            
            wpfEmailer.To = odbiorcy;
            if (wpfEmailer.wyslij() == 1)
            {
                busyIdicator.IsBusy = true;
            }
            
            
        }
        

        void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            busyIdicator.IsBusy = false;
            this.Close();
            MessageBox.Show("Wiadomość została wysłana poprawnie", "Sukces", MessageBoxButton.OK, MessageBoxImage.Information);

            
        }

        private void btnAnuluj_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }      

        private void btnAnulujWysylanie_Click(object sender, RoutedEventArgs e)
        {
            wpfEmailer.client.SendAsyncCancel();
            busyIdicator.IsBusy = false;
            MessageBox.Show("Wysyłanie anulowane", "Informacja", MessageBoxButton.OK, MessageBoxImage.Warning);
        }

        private void tbDodajZalacznik_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
            // Ustawianie rodzaju plików
            //dialog.Filter = "Obrazy (*.jpg,*.png) |*.jpg;*.png";
            //dialog.Multiselect = true;


            if (dialog.ShowDialog() == true)
            {
                FileInfo fi = new FileInfo(dialog.FileName);
                long size = fi.Length / 1024;
                if (size > 2048)
                {
                    MessageBox.Show("Maksymalny rozmiar załącznika to 2MB", "Ostrzeżenie", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                else
                {

                    wpfEmailer.AttachmentPath = dialog.FileName;
                    tbSciezkaPlik.Text = dialog.FileName;
                }
            }
        }      
    }
}
